"""
Case Type   : 资源池化集群系统支持归档备份
Case Name   : 设置archive_mode为on、archive_command设置错误,archive_dest无权限
Create At   : 2024/04/19
Owner       : @kyleze
Description :
    1.备份配置文件
    2.设置archive_mode=on
    3.设置archive_dest='/root/archive_dir', archive_command='dsscmd ls %p %f'
    4.执行pg_switch_xlog
    5.查看归档文件是否存在
Expect      :
    1.备份配置文件
    2.成功
    3.成功
    4.成功
    5.不生成归档文件
History     :
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "dms_res" not in Common().get_sh_result(
        Node("PrimaryDbUser"),
        f"source {macro.DB_ENV_PATH};cm_ctl query -Cvidp"), "非资源池化不执行")
class SharedStorageAchiveXlog(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"----{os.path.basename(__file__)} start----")
        self.constant = Constant()
        self.com = Common()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.primary_node = Node("PrimaryDbUser")

    def test_archive_xlog(self):
        step = "--step1:备份配置文件 expect:成功--"
        self.logger.info(step)
        cmd = f"cp " \
              f"{os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.conf')} " \
              f"{os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.confbak')}"
        self.logger.info(cmd)
        result = self.primary_node.sh(cmd).result()
        self.logger.info(result)
        self.assertEqual("", result, "执行失败:" + step)

        step = "--step2:设置archive_mode=on expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode=on")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step3:设置archive_dest='/root/archive_dir', " \
               "archive_command='dsscmd ls %p %f' expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='/root/archive_dir'")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_command='dsscmd ls %p %f'")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step4:执行pg_switch_xlog expect:成功--"
        self.logger.info(step)
        result = self.primary_sh.execut_db_sql(
            "select pg_switch_xlog();")
        self.logger.info(result)
        self.assertIn('1 row', result, "执行失败:" + step)

        step = "--step5:查看归档文件是否存在 expect:不生成归档文件--"
        self.logger.info(step)
        cmd = f"ls /root/archive_dir"
        self.logger.info(cmd)
        ls_archive = self.primary_node.sh(cmd).result()
        self.logger.info(ls_archive)
        self.assertIn("Permission denied", ls_archive, "执行失败:" + step)

    def tearDown(self):
        self.logger.info("----清理环境----")
        cmd = "rm -rf " \
            f"{os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.conf')};" \
            f"mv {os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.confbak')}" \
            f" {os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.conf')}"
        conf_res = self.primary_node.sh(cmd).result()
        self.logger.info(conf_res)
        stop_res = self.primary_sh.exec_cm_ctl("stop")
        self.logger.info(stop_res)
        self.assertIn(self.constant.cm_stop_success_msg, stop_res,
                      "执行失败:" + stop_res)
        start_res = self.primary_sh.exec_cm_ctl("start")
        self.logger.info(start_res)
        self.assertIn(self.constant.cm_stop_success_msg, stop_res,
                      "执行失败:" + stop_res)
        self.assertIn(self.constant.cm_start_success_msg, start_res,
                      "执行失败:" + start_res)
        self.assertEqual("", conf_res, "还原配置文件失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
